#!/bin/bash
myServer="localhost";
myLogin="";
myPasswd="";
conf="trixbi/conf/trixbi.conf.php";

clear
echo -e "\033[4mTrixBi v0.01\033[0m";
echo;
tput sgr0;

if ! [ -d /etc/trixbox ]; then
	echo "Error: Trixbox is not installed in this system!";
	exit;
fi

echo -n "MySQL login: ";
	read myLogin;
while :
do
	echo -n "MySQL password: ";
	OLDSTTY=`stty -g`;
	stty -echo;
	read myPasswd1;
	stty $OLDSTTY;
	echo;
	echo -n "Re-type password: ";
	OLDSTTY=`stty -g`;
	stty -echo;
	read myPasswd2;
	echo;
	stty $OLDSTTY;
	if [ $myPasswd1 = $myPasswd2 ]; then
		myPasswd=$myPasswd1;
		break;
	else
		echo "Error: Passwords are not equal!";
		echo;
		continue;
	fi
done;

while :
do
	echo -n "Installation path: ";
	read instPath;
	if ! [ -d $instPath ]; then
		echo "Error: Wrong path!";
		echo;
		continue;
	else
		break;
	fi
done;

echo "<?php" > $conf;
echo "/*" >> $conf;
echo "Server="$myServer >> $conf;
echo "User="$myLogin >> $conf;
echo "Passwd="$myPasswd >> $conf;

while :
do
	echo -n "Language(Available: russian lang - ru): ";
	read lang;
	case $lang in
		ru)
		echo "language="$lang >> $conf;
		break;
		;;
		*)
		echo "Error: language not available";
		continue;
		;;
	esac;
done;

echo "*/" >> $conf;
echo "?>" >> $conf;
mysqladmin -u $myLogin -p$myPasswd create trixbi;
mysql -u $myLogin -p$myPasswd trixbi < trixbi.sql;

cp -R trixbi/ $instPath;
chmod +x $instPath/trixbi;
cp -R module/ /var/www/html/user/modules/05_TrixBi;
rm -rf $conf;
echo;
echo "TrixBi succesfuly installed."
